package com.bdcom.autotest.telnet.invoke.impl;

import com.bdcom.autotest.telnet.TelnetClientForCOM;
import com.bdcom.autotest.telnet.TelnetCommunication;
import com.bdcom.autotest.telnet.constant.TelnetConstant;
import com.bdcom.autotest.telnet.invoke.DeveiceOper;
import com.bdcom.autotest.telnet.model.Result;
import com.bdcom.autotest.telnet.model.TelnetCMDObj;
import com.bdcom.autotest.telnet.util.TelnetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备从config模式进入monitor模式，
 * 本来就是monitor模式不直接返回true
 */
public class DevMonitor implements DeveiceOper , TelnetConstant {
    private static final Logger logger = LoggerFactory.getLogger(DevMonitor.class);
    @Override
    public Result executeCMD(TelnetCMDObj obj) {
        Result result=new Result();
        TelnetClientForCOM clientForCOM=null;
        try {
            TelnetCommunication communication=new TelnetCommunication();
            clientForCOM=new TelnetClientForCOM(obj.getIp(),obj.getPort());
            String curPattern=communication.obtainCurrentPattern(clientForCOM);
            if(null!=curPattern&&curPattern.equals(MONITOR_PATTERN)){
                //monitor模式下也需要format，防止重启后进入master模式
                String connect = clientForCOM.connect(EXEC_CONNECT_TIME);
                String formatRet = clientForCOM.sendCommand("format", 3000L);
                logger.info("formatRet in monitor:"+formatRet);
                if(formatRet.contains("y/n")){
                    boolean flag=communication.sendMonitorFormat(clientForCOM);
                    if(flag){
                        result.setStatus(true);//当前就是Monitor模式,且已经format成功
                        return result;
                    }else{
                        result.setStatus(false);//当前就是Monitor模式,但format失败
                        result.setFailedReason("timeout");
                        return result;
                    }
                }else{
                    result.setStatus(false);
                    result.setFailedReason("execute monitor format,can't obtain select y/n,formatRet:"+formatRet);
                    return result;
                }

            }
            Result loginResult=communication.login(obj.getUser(),obj.getPassword(),clientForCOM,obj.isMonitor());
            if(loginResult.getStatus()){
                Result enableResult = communication.enableOper(clientForCOM, obj.getEnablePassword(),obj.isMonitor());
                if(enableResult.getStatus()){
                    String formatRet = clientForCOM.sendCommand(obj.getCmds().get(0), 3000L);
                    logger.info("formatRet:"+formatRet);
                    if(formatRet.contains("y/n")){
                        formatRet=communication.sendCommandWithRetry(clientForCOM,EXEC_FORMAT_TIME,EXEC_FORMAT_RETRY_TIME,"y",EndFlagsForFormatReboot);
                        result= TelnetUtil.executeStatusForFormatReboot(formatRet,"y");
                        if(result.getStatus()){
                            //先回车
                            clientForCOM.sendCommand("\r\n",EXEC_ENTER_TIME);
                            //执行reboot
                            String rebootRet = clientForCOM.sendCommand(obj.getCmds().get(1), 1000L);
                            logger.info("rebootRet:"+formatRet);
                            if(rebootRet.contains("y/n")){
                                rebootRet=communication.sendCommandWithRetry(clientForCOM,EXEC_REBOOT_TIME,EXEC_REBOOT_RETRY_TIME,"y",EndFlagsForFormatReboot);
                                result= TelnetUtil.executeStatusForFormatReboot(rebootRet,"y");
                                return result;


                            }else{
                                result.setStatus(false);
                                result.setFailedReason("execute master reboot,can't obtain select y/n");
                                return result;
                            }


                        }else{
                            result.setFailedReason("format failed");
                            return result;
                        }

                    }else{
                        result.setStatus(false);
                        result.setFailedReason("execute master format,can't obtain select y/n");
                        return result;
                    }





                }else{
                    return enableResult;
                }
            }else{
                return loginResult;
            }
            

        }catch (Exception e){
            result.setFailedReason(e.toString());
            return result;
        }finally {
            if(null!=clientForCOM){
                try {

                    if(!obj.isMonitor()){
                        //非monitor模式下恢复telnet连接的COM口的初始状态
                        // clientForCOM.sendCommand("exit",200L);
                        //clientForCOM.sendCommand("exit",200L);
                    }

                    clientForCOM.disConnect();
                } catch (Exception e) {
                    logger.error(e.toString());
                }
            }
        }


    }
    public Result switchMonitor(TelnetCMDObj obj){
        obj.setMonitor(false);//只能是非monitor下操作
        List<String> cmds=new ArrayList<String>();
        cmds.add("format");
        cmds.add("reboot");
        obj.setCmds(cmds);
        Result result = executeCMD(obj);
        return result;


    }
}
